package org.bongo.open.cumulo.core.utils;

import java.util.ArrayList;
import java.util.List;

public class LazyIndexedObjectPool<T> {
	private List<T> objectPool;
	PoolableObjectFactory<T> objectFactory;
	
	
	public LazyIndexedObjectPool(PoolableObjectFactory<T> factory) {
		objectFactory = factory;
	}
	
	public T obtainFromPool(int index) {
		if(objectPool == null)
			objectPool = new ArrayList<T>();
		
		if(objectPool.size() == index) {
			T b = objectFactory.createObject();
			objectPool.add(b);
		}
		
		return objectPool.get(index);
	}
	
	public void evictAll() {
		for(T object: objectPool) {
			objectFactory.cleanup(object);
		}
	}
}


